<?php
/**
 * Created by PhpStorm.
 * User: Liu
 * Date: 2022/5/25
 * Time: 9:24
 */

namespace app\admin\controller;


use app\admin\common\Base;

class Faq extends Base {
    public function index($page=1,$limit=10){
        if (is_post()){
            $where[] = ['is_del','=',0];

            $data = $this->getListData('faq',$where,'sort','asc',$page,$limit);

            return json(['code'=>'suc','msg'=>'数据请求成功','count'=>$data['count'],'data'=>$data['list'],'current_page'=>$page]);
        }
        return $this->fetch();
    }

    public function add(){
        if (is_post()){
            $data = $this->handleData(input('post.'),'add');
            $validate = $this->validateData($data);

            if ($validate['code'] == 'suc'){
                $result = db('faq')->insertGetId($data);
                if ($result){
                    insert_system_log('添加FAQ：' . $result);
                    return json(['code' => 'suc', 'msg' => '添加成功']);
                }else {
                    return json(['code' => 'err', 'msg' => '系统繁忙，请重试']);
                }
            }else{
                return json(['code' => $validate['code'], 'msg' => $validate['msg']]);
            }
        }
        return $this->fetch();
    }

    public function edit($id){
        if (is_post()){
            $data = $this->handleData(input('post.'),'edit');
            $validate = $this->validateData($data);

            if ($validate['code'] == 'suc'){
                $result = db('faq')
                    ->where('id',$id)
                    ->update($data);
                if ($result){
                    return json(['code' => 'suc', 'msg' => '修改成功']);
                }else {
                    return json(['code' => 'err', 'msg' => '系统繁忙，请重试']);
                }
            }else{
                return json(['code' => $validate['code'], 'msg' => $validate['msg']]);
            }
        }
        $info = db('faq')
            ->where('id',$id)
            ->find();
        $this->assign('info',$info);
        return $this->fetch();
    }

    public function del($id){
        if (is_post()){
            if ($this->delRecord('faq',$id,'删除FAQ：')){
                return json(['code' => 'suc','msg' => '已删除']);
            }
            return json(['code' => 'err','msg' => '系统繁忙，请重试']);
        }
    }

    private function handleData($data,$action){
        if ($action == 'add'){
            $data['create_time'] = time();
        }
        $data['update_time'] = time();
        $data['status'] = empty($data['status']) ? 0 : 1;

        return $data;
    }

    private function validateData($data){
        $code = 'suc';
        $msg = '';
        if (empty($data['title'])){
            $code = 'err';
            $msg = '问题标题不能为空';
        }elseif (empty($data['answer'])){
            $code = 'err';
            $msg = '回答不能为空';
        }

        return ['code' => $code,'msg' => $msg];
    }

    public function changeStatus($type,$id){
        if (is_post()){
            if ($this->changeRecordStatus('faq',$id,1,$type)){
                if ($type == 1){
                    $msg = '已显示';
                    $log_msg = '显示FAQ：';
                }else{
                    $msg = '已隐藏';
                    $log_msg = '隐藏FAQ：';
                }
                insert_system_log($log_msg);
                return json(['code' => 'suc','msg' => $msg]);
            }
            return json(['code' => 'err','msg' => '系统繁忙，请重试']);
        }
    }
}